<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\Activity;
use App\Model\ActivityEnroll;
use App\Model\ActivityContribute;

class ActivityService extends BaseService
{

    public function list(){
        $model = Activity::query()->with('activityCategory')->orderBy('begin_time', 'DESC')->where('status', 1)->where('deleted', 0);
        $list = $this->usePage($model)->toArray();

        foreach($list['data'] as $key=>$val)
        {
            $list['data'][$key]['content'] = mb_substr(strip_tags($val['content']), 0, 80);
            $list['data'][$key]['status'] = $this->checkTimeStatus($val['begin_time'], $val['end_time']);
        }

        return $list;
    }

    public function detail(array $params)
    {
        $activity = Activity::query()->with('activityCategory')->with('activityRelation')->where('id', $params['id'])->where('status', 1)->where('deleted', 0)->first();
        if(!$activity)
        {
            throw new ApiException(ErrorCode::WARNING, '活动不存在');
        }
        $activity['status'] = $this->checkTimeStatus($activity['begin_time'], $activity['end_time']);

        if($activity['category_id']!=1)        //话题活动无报名按钮
        {
            $activity['anroll_btn'] = $this->checkTime($activity['begin_time'], $activity['contribute_time']);
        }

        $activity['contribute_btn'] = $this->checkTime($activity['contribute_time'], $activity['end_time']);

        return $activity;
    }

    public function anroll(array $params)
    {
        $user = auth_user();

        $enroll = ActivityEnroll::query()->select('id')->where('user_id', $user['id'])->where('activity_id', $params['id'])->first();
        if($enroll)
        {
            throw new ApiException(ErrorCode::WARNING, '当前活动已报名');
        }

        ActivityEnroll::query()->insert(['user_id'=>$user['id'], 'activity_id'=>$params['id']]);

        return [];
    }

    public function contribute(array $params)
    {
        $user = auth_user();

        $contribute = ActivityContribute::query()->select('id')->where('user_id', $user['id'])->where('activity_id', $params['id'])->first();
        if($contribute)
        {
            throw new ApiException(ErrorCode::WARNING, '当前活动已投稿');
        }

        $data['user_id'] = $user['id'];
        $data['activity_id'] = $params['id'];
        $data['relation_id'] = $params['relation_id'];
        $data['url'] = $params['url'];
        ActivityContribute::query()->insert($data);

        return [];
    }

    public function checkTimeStatus($beginTime, $endTime)
    {
        $currentTime = date('Y-m-d H:i:s');
        // 判断状态
        if ($currentTime < $beginTime) {
            $status = "未开始";
        } elseif ($currentTime >= $beginTime && $currentTime <= $endTime) {
            $status = "进行中";
        } else {
            $status = "已结束";
        }
        return $status;
    }

    //检查时间范围
    public function checkTime($beginTime, $endTime)
    {
        $currentTime = date('Y-m-d');
        // 判断状态
        $status = 0;
        if ($currentTime>$beginTime && $currentTime<$endTime) {
            $status = 1;
        }
        return $status;
    }

}